//
//  YTTableViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/4/27.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTTableViewController.h"
#import <YYWebImage/YYWebImage.h>
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

@interface YTTableViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@end

@implementation YTTableViewController

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    YTTableViewController *tableViewController = [super allocWithZone:zone];
    @weakify(tableViewController);
    [[tableViewController rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x) {
        @strongify(tableViewController);
        [tableViewController initialize];
        [tableViewController bindViewModel];
    }];
    return tableViewController;
}

- (void)dealloc {
    NSLog(@"%@ was dealloc", NSStringFromClass(self.class));
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
    @weakify(self);
    [[self rac_signalForSelector:@selector(viewWillAppear:)] subscribeNext:^(id x) {
        @strongify(self);
        if (self.naviType == YTNavigationBarBlueStyle) {
            [self yt_changStatusBarWithStyle:UIStatusBarStyleLightContent];
        } else {
            [self yt_changStatusBarWithStyle:UIStatusBarStyleDefault];
        }
    }];
}

- (void)initialize {}

- (void)bindViewModel {}

- (void)setNaviType:(YTNavigationBarStyle)naviType {
    _naviType = naviType;
    switch (self.naviType) {
        case YTNavigationBarBlueStyle: {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage yy_imageWithColor:kMainBlueColor] forBarMetrics:0];
            [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
            self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        }
            break;
        case  YTNavigationBarClearStyle: {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage yy_imageWithColor:[UIColor clearColor]] forBarMetrics:0];
            [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
            self.navigationController.navigationBar.tintColor = [UIColor clearColor];
        }
            break;
        case YTNavigationBarDefaultStyle:
            self.navigationController.navigationBar.tintColor = [UIColor blackColor];
            break;
        default:
            self.navigationController.navigationBar.tintColor = [UIColor blackColor];
            break;
    }
}

/** 下拉刷新*/
- (void)addRefreshControl:(void (^)())callBack {
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:callBack];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    [header.arrowView setHidden:YES];
    self.tableView.mj_header = header;
}

/** 上拉加载*/
- (void)addLoadDataControl:(void (^)())callBack {
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:callBack];
    footer.triggerAutomaticallyRefreshPercent = 0.1;
    footer.automaticallyHidden = YES;
    [footer setTitle:@"*^_^* 没了呀..." forState:MJRefreshStateNoMoreData];
    [footer setTitle:@"正在加载中..." forState:MJRefreshStateRefreshing];
    self.tableView.mj_footer = footer;
}

- (void)reloadData {
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - DZNEmptyDataSetSource

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = self.emptyDataTips;
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f],
                                 NSForegroundColorAttributeName: kGrayColor};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:self.emptyDataImage];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -100;
}

@end
